(function ($) {		
	$.fn.toAjaxLink = function (options) {		
		return this.each(function () {	
			options = $.extend({	
					url:"",						
					toId:"mainbody",
					param:"",
					form:"",
					js:"",
					css:""
				}, options || {});
			var href = $(this).attr("href");
					
			if(href != null && href != '' && href != '#' ){					
				$(this).attr("id",href).attr("href","#");											
				$(this).click(function () {					
				    var ajaxid = $(this).attr("ajaxid");
				    if(ajaxid == null || ajaxid == '' ){				    	
				    	var href_array = href.split("#");
				    	var href_array_length = href_array.length;
				    	if(href_array.length >=2){
				    		ajaxid = href_array[href_array_length-1];				    		
				    	} 				    	
				    }
				    if(ajaxid == null || ajaxid == '' ){
				    	ajaxid = options.toId;
				    }
					var url = $.joinUrlWithParam((options.url === null || options.url === "" || options.url === "#" )?$(this).attr("id"):options.url,options.param);							
					if (FileLoader.autoLoad($(this).attr("id"),$(this).attr("js"), $(this).attr("css")) == 1
						&& FileLoader.autoLoad($(this).attr("id"),options.js, options.css) === 1) {					
						if(options.form === null || options.form === ""){
							$.ajaxRequest(ajaxid,url);
						}else{
							$.ajaxFormRequest(ajaxid,url,options.form);
						}
					}
					return false;
				});
			}
		});
	};
	$.fn.toAjaxSelect = function (options) {
		return this.each(function () {
			options = $.extend({								
					toId:"mainbody",
					url:"",					
					param:"",
					form:"",
					js:"",
					css:"",
					field:"",
					mutilSelect:false,
					defaultValue:null
				}, options || {});					
			$(this).change(function () {		
				var url = $.joinUrlWithParam((options.url == null || options.url == "" )?$(this).attr("id"):options.url,options.param);
				url = $.joinUrlWithParam(url,options.field + "=" + $(this).val());															
				if (FileLoader.autoLoad($(this).attr("id"),$(this).attr("js"), $(this).attr("css")) == 1
					&& FileLoader.autoLoad($(this).attr("id"),options.js, options.css) == 1) {					
					if(options.form == null || options.form == ""){
						if(options.mutilSelect == true){
							$.ajaxMutilSelect(options.toId,url,'',null,null,options.defaultValue);
						}else{
							$.ajaxRequest(options.toId,url);
						}
					}else{
						$.ajaxFormRequest(options.toId,url,options.form);
					}
				}			
			});
		});
	};	
	$.fn.toAjaxPagination = function (options) {
		return this.each(function () {
			options = $.extend({
					url:"",								
					toId:"mainbody",
					param:""
				}, options || {});				
			$(this).find("a").click(function () {
				var param = "page=" + $(this).attr("page") + "&count=" + $(this).attr("count");
				$.ajaxRequest(options.toId, $.joinUrlWithParam($.joinUrlWithParam(options.url,param),options.param));				
			});
		});
	};	
	
	$.fn.toCssTable = function (options) {
		return this.each(function () {					
			$(this).find("tr").hover(
		      function () {
		        $(this).toggleClass("highlight");
		      }, 
		      function () {
		        $(this).removeClass("highlight");
		      }
		    );
		});
	};	
	$.fn.toSortField = function (options) {
		return this.each(function () {
			options = $.extend({													
					param: "",
					toId: "mianbody",
					url: "",					
					orderby: ""
				}, options || {});	
			var _name = options.orderby.split("_");	
			var orderby_by = (_name.length == 1)?options.orderby:_name[0];	
			var orderby_order = (_name.length == 1)?"ASC":_name[1];				
			if($(this).attr("id") != orderby_by){
				var orderby_url = $(this).attr("id") + "_ASC";
				$(this).prepend("<img class='up' src='public/images/icons/refresh.gif'>");
			}else if(orderby_order.toUpperCase() == "DESC"){
				var orderby_url = $(this).attr("id") + "_ASC";
				$(this).prepend("<img class='up' src='public/images/icons/up-arrow.gif'>");
			}else{	
				var orderby_url = $(this).attr("id") + "_DESC";			
				$(this).prepend("<img class='up' src='public/images/icons/down-arrow.gif'>");
			}			
			$(this).click(function () {				
				var url = $.joinUrlWithParam($.joinUrlWithParam(options.url,options.param),"orderby=" + orderby_url);				
				$.ajaxRequest(options.toId, url);			
			});
		});
	};	
})(jQuery);

